package com.handu.luna.mall.controller;

import com.handu.luna.core.domain.goods.*;

import org.apache.commons.lang.StringUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


/**
 * Created by wangzailiang on 2014/9/9.
 */
@Controller
@RequestMapping("/goods/*")
public class GoodsController extends BaseController {

    @RequestMapping(value = "{id}")  //size = DEFAULT_PAGE_SIZE
	public MallModelView index(@PathVariable long id){

       Goods goods = context.getGoodsRepository().findOne(id);
        if(null == goods) {
            throw new PageNotFoundException("商品不存在");
        }
        currentUser = context.getUserRepository().findOne(2L);
        //初始化用户
        currentUser.initUserRank(currentUser);
        String rankName = currentUser.getRankName();

        goods.initGoodsInfo(currentUser);

        //取得商品相册数据
        Page<Album> albumList = context.getAlbumRepository().findByGoodsId(goods.getId(),new PageRequest(0,4));

        long brandId = goods.getBrandId();
        @SuppressWarnings("unused")
		Brand brand = context.getBrandRepository().findOne(brandId);
        long categoryId = goods.getCategoryId();
        @SuppressWarnings("unused")
		Category category = context.getCategoryRepository().findOne(categoryId);
        //爆款top10(同品牌 按销量 10款)
        Page<Goods> brandSalesGoodsList = context.getGoodsRepository().findSalesTopNByBrandId(brandId,new PageRequest(0,10));
        goods.initShowPrice(brandSalesGoodsList, currentUser);
        //人气top10(同品牌 按新品 10款)
        Page<Goods> brandNewGoodsList = context.getGoodsRepository().findNewTopNByBrandId(brandId, new PageRequest(0, 10));
        goods.initShowPrice(brandNewGoodsList,currentUser);
        //看了又看(同类目，按销量，9张图片，轮播3张,3屏)
        Page<Goods> categorySalesGoodsList = context.getGoodsRepository().findSalesTopNByCategoryId(categoryId, new PageRequest(0,9));
        goods.initShowPrice(categorySalesGoodsList,currentUser);
        //猜你喜欢(同类目，按新品，10张图片，不轮播放2排)
        Page<Goods> categoryNewGoodsList = context.getGoodsRepository().findNewTopNByCategoryId(categoryId, new PageRequest(0,10));
        goods.initShowPrice(categoryNewGoodsList,currentUser);
        //广告位1
        //广告位2
        //广告位3
        //搭配
        //热门分类


        return buildModelView("/goods/goods")
                .add("rankName",rankName)
                .add("goods",goods)
                .add("albumList",albumList)
                .add("brandSalesGoodsList",brandSalesGoodsList)
                .add("brandNewGoodsList",brandNewGoodsList)
                .add("categorySalesGoodsList",categorySalesGoodsList)
                .add("categoryNewGoodsList",categoryNewGoodsList);
    }


    @RequestMapping(value = "checksku", produces = {"application/json;charset=UTF-8"})
    public @ResponseBody String searchRegion(long goodsid,String color,String size) {
        String attr = "";
        if(StringUtils.isNotEmpty(color) && StringUtils.isNotEmpty(size)) {
            attr = new StringBuilder(color).append(",").append(size).toString();
        }

        SKU sku = context.getSkuRepository().findByGoodsIdAndAttributeValues(goodsid,attr);
        return String.valueOf(sku.getGoodsNum());

    }
}
